using System.Runtime.InteropServices;
using UnityEngine;
using UnityEngine.Networking;

public class CharacterSkyDriving : NetworkBehaviour
{
	public GameObject Parachute;

	private CharacterSystem character;

	private PlayerView view;

	public float UpForce = 10f;

	public float ViewDistance = 10f;

	private bool isJumpped;

	private bool isGrounded;

	public bool IsGrounded;

	[SyncVar]
	public bool Released;

	private float timeTmp;

	public float TimeReleaseParachute = 5f;

	private float timeReleaseTmp;

	private static int kCmdCmdJumpOutThePlane;

	private static int kRpcRpcJumpOutThePlane;

	private static int kCmdCmdReleaseParachute;

	private static int kCmdCmdLanded;

	private static int kRpcRpcLanded;

	public bool NetworkReleased
	{
		get
		{
			return Released;
		}
		[param: In]
		set
		{
			SetSyncVar(value, ref Released, 1u);
		}
	}

	private void Awake()
	{
		view = GetComponent<PlayerView>();
		character = GetComponent<CharacterSystem>();
	}

	private void Start()
	{
		isJumpped = false;
		isGrounded = false;
	}

	public void OnOutOfVehicle(bool needparachute)
	{
		if (needparachute)
		{
			CallCmdJumpOutThePlane();
		}
	}

	[Command(channel = 0)]
	private void CmdJumpOutThePlane()
	{
		if (!isJumpped)
		{
			character.Sit(2);
			CallRpcJumpOutThePlane();
			timeTmp = Time.time;
			isJumpped = true;
			isGrounded = false;
			timeReleaseTmp = Time.time;
		}
	}

	[ClientRpc(channel = 0)]
	private void RpcJumpOutThePlane()
	{
		isJumpped = true;
		isGrounded = false;
		timeReleaseTmp = Time.time;
		if (view != null)
		{
			view.View = PlayerViewType.FreeView;
			view.OrbitDistance = ViewDistance;
		}
	}

	public void ReleaseParachute()
	{
		if (!Released && isJumpped && !isGrounded)
		{
			CallCmdReleaseParachute();
			character.Sit(0);
			NetworkReleased = true;
		}
	}

	[Command(channel = 0)]
	private void CmdReleaseParachute()
	{
		NetworkReleased = true;
		character.Sit(0);
	}

	[Command(channel = 0)]
	private void CmdLanded()
	{
		isGrounded = true;
		character.Sit(0);
		CallRpcLanded();
	}

	[ClientRpc(channel = 0)]
	private void RpcLanded()
	{
		isGrounded = true;
		NetworkReleased = false;
		if (view.View == PlayerViewType.FreeView)
		{
			view.View = PlayerViewType.FirstVeiw;
		}
	}

	private void Update()
	{
		if (character == null || character.Motor == null)
		{
			return;
		}
		IsGrounded = character.Motor.grounded;
		if ((bool)Parachute)
		{
			Parachute.SetActive(Released);
		}
		if (base.isServer)
		{
			if (isJumpped && !isGrounded)
			{
				if (Time.time > timeTmp + 2f && IsGrounded)
				{
					CallCmdLanded();
				}
				if (!Released && Time.time >= timeReleaseTmp + TimeReleaseParachute)
				{
					ReleaseParachute();
				}
			}
		}
		else if (isJumpped && !isGrounded)
		{
			view.OrbitDistance = ViewDistance;
		}
		if (Released && base.isLocalPlayer)
		{
			base.transform.position += Vector3.up * Time.deltaTime * UpForce;
		}
	}

	private void UNetVersion()
	{
	}

	protected static void InvokeCmdCmdJumpOutThePlane(NetworkBehaviour obj, NetworkReader reader)
	{
		if (!NetworkServer.active)
		{
			UnityEngine.Debug.LogError("Command CmdJumpOutThePlane called on client.");
		}
		else
		{
			((CharacterSkyDriving)obj).CmdJumpOutThePlane();
		}
	}

	protected static void InvokeCmdCmdReleaseParachute(NetworkBehaviour obj, NetworkReader reader)
	{
		if (!NetworkServer.active)
		{
			UnityEngine.Debug.LogError("Command CmdReleaseParachute called on client.");
		}
		else
		{
			((CharacterSkyDriving)obj).CmdReleaseParachute();
		}
	}

	protected static void InvokeCmdCmdLanded(NetworkBehaviour obj, NetworkReader reader)
	{
		if (!NetworkServer.active)
		{
			UnityEngine.Debug.LogError("Command CmdLanded called on client.");
		}
		else
		{
			((CharacterSkyDriving)obj).CmdLanded();
		}
	}

	public void CallCmdJumpOutThePlane()
	{
		if (!NetworkClient.active)
		{
			UnityEngine.Debug.LogError("Command function CmdJumpOutThePlane called on server.");
			return;
		}
		if (base.isServer)
		{
			CmdJumpOutThePlane();
			return;
		}
		NetworkWriter networkWriter = new NetworkWriter();
		networkWriter.Write((short)0);
		networkWriter.Write((short)5);
		networkWriter.WritePackedUInt32((uint)kCmdCmdJumpOutThePlane);
		networkWriter.Write(GetComponent<NetworkIdentity>().netId);
		SendCommandInternal(networkWriter, 0, "CmdJumpOutThePlane");
	}

	public void CallCmdReleaseParachute()
	{
		if (!NetworkClient.active)
		{
			UnityEngine.Debug.LogError("Command function CmdReleaseParachute called on server.");
			return;
		}
		if (base.isServer)
		{
			CmdReleaseParachute();
			return;
		}
		NetworkWriter networkWriter = new NetworkWriter();
		networkWriter.Write((short)0);
		networkWriter.Write((short)5);
		networkWriter.WritePackedUInt32((uint)kCmdCmdReleaseParachute);
		networkWriter.Write(GetComponent<NetworkIdentity>().netId);
		SendCommandInternal(networkWriter, 0, "CmdReleaseParachute");
	}

	public void CallCmdLanded()
	{
		if (!NetworkClient.active)
		{
			UnityEngine.Debug.LogError("Command function CmdLanded called on server.");
			return;
		}
		if (base.isServer)
		{
			CmdLanded();
			return;
		}
		NetworkWriter networkWriter = new NetworkWriter();
		networkWriter.Write((short)0);
		networkWriter.Write((short)5);
		networkWriter.WritePackedUInt32((uint)kCmdCmdLanded);
		networkWriter.Write(GetComponent<NetworkIdentity>().netId);
		SendCommandInternal(networkWriter, 0, "CmdLanded");
	}

	protected static void InvokeRpcRpcJumpOutThePlane(NetworkBehaviour obj, NetworkReader reader)
	{
		if (!NetworkClient.active)
		{
			UnityEngine.Debug.LogError("RPC RpcJumpOutThePlane called on server.");
		}
		else
		{
			((CharacterSkyDriving)obj).RpcJumpOutThePlane();
		}
	}

	protected static void InvokeRpcRpcLanded(NetworkBehaviour obj, NetworkReader reader)
	{
		if (!NetworkClient.active)
		{
			UnityEngine.Debug.LogError("RPC RpcLanded called on server.");
		}
		else
		{
			((CharacterSkyDriving)obj).RpcLanded();
		}
	}

	public void CallRpcJumpOutThePlane()
	{
		if (!NetworkServer.active)
		{
			UnityEngine.Debug.LogError("RPC Function RpcJumpOutThePlane called on client.");
			return;
		}
		NetworkWriter networkWriter = new NetworkWriter();
		networkWriter.Write((short)0);
		networkWriter.Write((short)2);
		networkWriter.WritePackedUInt32((uint)kRpcRpcJumpOutThePlane);
		networkWriter.Write(GetComponent<NetworkIdentity>().netId);
		SendRPCInternal(networkWriter, 0, "RpcJumpOutThePlane");
	}

	public void CallRpcLanded()
	{
		if (!NetworkServer.active)
		{
			UnityEngine.Debug.LogError("RPC Function RpcLanded called on client.");
			return;
		}
		NetworkWriter networkWriter = new NetworkWriter();
		networkWriter.Write((short)0);
		networkWriter.Write((short)2);
		networkWriter.WritePackedUInt32((uint)kRpcRpcLanded);
		networkWriter.Write(GetComponent<NetworkIdentity>().netId);
		SendRPCInternal(networkWriter, 0, "RpcLanded");
	}

	static CharacterSkyDriving()
	{
		kCmdCmdJumpOutThePlane = 1652958012;
		NetworkBehaviour.RegisterCommandDelegate(typeof(CharacterSkyDriving), kCmdCmdJumpOutThePlane, InvokeCmdCmdJumpOutThePlane);
		kCmdCmdReleaseParachute = -268697943;
		NetworkBehaviour.RegisterCommandDelegate(typeof(CharacterSkyDriving), kCmdCmdReleaseParachute, InvokeCmdCmdReleaseParachute);
		kCmdCmdLanded = 1530892857;
		NetworkBehaviour.RegisterCommandDelegate(typeof(CharacterSkyDriving), kCmdCmdLanded, InvokeCmdCmdLanded);
		kRpcRpcJumpOutThePlane = 394074278;
		NetworkBehaviour.RegisterRpcDelegate(typeof(CharacterSkyDriving), kRpcRpcJumpOutThePlane, InvokeRpcRpcJumpOutThePlane);
		kRpcRpcLanded = -1982005873;
		NetworkBehaviour.RegisterRpcDelegate(typeof(CharacterSkyDriving), kRpcRpcLanded, InvokeRpcRpcLanded);
		NetworkCRC.RegisterBehaviour("CharacterSkyDriving", 0);
	}

	public override bool OnSerialize(NetworkWriter writer, bool forceAll)
	{
		if (forceAll)
		{
			writer.Write(Released);
			return true;
		}
		bool flag = false;
		if ((base.syncVarDirtyBits & 1) != 0)
		{
			if (!flag)
			{
				writer.WritePackedUInt32(base.syncVarDirtyBits);
				flag = true;
			}
			writer.Write(Released);
		}
		if (!flag)
		{
			writer.WritePackedUInt32(base.syncVarDirtyBits);
		}
		return flag;
	}

	public override void OnDeserialize(NetworkReader reader, bool initialState)
	{
		if (initialState)
		{
			Released = reader.ReadBoolean();
			return;
		}
		int num = (int)reader.ReadPackedUInt32();
		if ((num & 1) != 0)
		{
			Released = reader.ReadBoolean();
		}
	}
}
